import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

public class Demo2 {

    private DatagramSocket d = null;
    private String ip;
    private int port;

    public Demo2(String ip, int port) throws SocketException {

        d = new DatagramSocket();
        this.ip = ip;
        this.port = port;
    }

    public void start() throws IOException {

        Scanner sc = new Scanner(System.in);

        while (true) {

            System.out.println("请输入请求：");
            if (!sc.hasNext()) {
                break;
            }
            String s1 = sc.next();
            DatagramPacket p1 =  new DatagramPacket(s1.getBytes(), s1.getBytes().length, InetAddress.getByName(ip) , port);
            d.send(p1);
            byte[] b = new byte[4096];
            DatagramPacket p2 = new DatagramPacket(b, 4096);
            d.receive(p2);
            String s = new String(p2.getData(), 0, p2.getLength());
            System.out.println(s);


        }


    }

    public static void main(String[] args) throws IOException {
        Demo2 demo2 = new Demo2("127.0.0.1", 6636);
        demo2.start();


    }
}
